In [1]:
import numpy as np
print(np.__version__)


1.13.0

In [2]:
b = np.array([[1,2,3,4],[5,6,7,8],[3,5,4,3]])

In [3]:
print(b)


[[1 2 3 4]
 [5 6 7 8]
 [3 5 4 3]]

In [4]:
print(b[:])


[[1 2 3 4]
 [5 6 7 8]
 [3 5 4 3]]

In [5]:
print(b[:,1])


[2 6 5]

In [6]:
print(b[-1])


[3 5 4 3]

In [7]:
print(b[-1,:])


[3 5 4 3]

In [8]:
print(b[-1,...])


[3 5 4 3]

In [9]:
print(b[0:2])


[[1 2 3 4]
 [5 6 7 8]]

In [11]:
print(b[0:1,:])


[[1 2 3 4]]

In [19]:
print(b[0:2, [-1]])


[[4]
 [8]]

In [20]:
print(b[0:2, -1])


[4 8]

In [22]:
print(b[0:2, [[-1]]])


[[[4]]

 [[8]]]

In [23]:
print(b)


[[1 2 3 4]
 [5 6 7 8]
 [3 5 4 3]]

In [29]:
print(b[-1:0:-1])


[[3 5 4 3]
 [5 6 7 8]]

In [30]:
print(b[-1::-1])


[[3 5 4 3]
 [5 6 7 8]
 [1 2 3 4]]

In [31]:
print(b[-1::-1, -1::-1])


[[3 4 5 3]
 [8 7 6 5]
 [4 3 2 1]]

In [32]:
print(b)


[[1 2 3 4]
 [5 6 7 8]
 [3 5 4 3]]

In [33]:
print(b[:, [-1]])


[[4]
 [8]
 [3]]

In [34]:
print(b[-1::-1, [-1]])


[[3]
 [8]
 [4]]

In [35]:
help(np.dot)


Help on built-in function dot in module numpy.core.multiarray:

dot(...)
    dot(a, b, out=None)
    
    Dot product of two arrays.
    
    For 2-D arrays it is equivalent to matrix multiplication, and for 1-D
    arrays to inner product of vectors (without complex conjugation). For
    N dimensions it is a sum product over the last axis of `a` and
    the second-to-last of `b`::
    
        dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])
    
    Parameters
    ----------
    a : array_like
        First argument.
    b : array_like
        Second argument.
    out : ndarray, optional
        Output argument. This must have the exact kind that would be returned
        if it was not used. In particular, it must have the right type, must be
        C-contiguous, and its dtype must be the dtype that would be returned
        for `dot(a,b)`. This is a performance feature. Therefore, if these
        conditions are not met, an exception is raised, instead of attempting
        to be flexible.
    
    Returns
    -------
    output : ndarray
        Returns the dot product of `a` and `b`.  If `a` and `b` are both
        scalars or both 1-D arrays then a scalar is returned; otherwise
        an array is returned.
        If `out` is given, then it is returned.
    
    Raises
    ------
    ValueError
        If the last dimension of `a` is not the same size as
        the second-to-last dimension of `b`.
    
    See Also
    --------
    vdot : Complex-conjugating dot product.
    tensordot : Sum products over arbitrary axes.
    einsum : Einstein summation convention.
    matmul : '@' operator as method with out parameter.
    
    Examples
    --------
    >>> np.dot(3, 4)
    12
    
    Neither argument is complex-conjugated:
    
    >>> np.dot([2j, 3j], [2j, 3j])
    (-13+0j)
    
    For 2-D arrays it is the matrix product:
    
    >>> a = [[1, 0], [0, 1]]
    >>> b = [[4, 1], [2, 2]]
    >>> np.dot(a, b)
    array([[4, 1],
           [2, 2]])
    
    >>> a = np.arange(3*4*5*6).reshape((3,4,5,6))
    >>> b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))
    >>> np.dot(a, b)[2,3,2,1,2,2]
    499128
    >>> sum(a[2,3,2,:] * b[1,2,:,2])
    499128


넘파이에서 의 array

파이썬의 리스트와는 다르게 배열의 모든 값은 같은 타입(예를들어, Boolean, integer, real, complex number)을 가져야 한다.


In [36]:
myarray = np.array([1,2,3])

In [37]:
print(myarray)


[1 2 3]

In [38]:
print(myarray.shape)


(3,)

In [39]:
print(type(myarray))


<class 'numpy.ndarray'>

In [40]:
l = [1,2,3]

In [41]:
print(type(l))


<class 'list'>

In [49]:
mybigarray = np.array([[3,2,4],[3,3,2],[4,5,2],[4,2,8,5]])

In [50]:
print(mybigarray.shape)


(4,)

In [51]:
print(mybigarray)


[list([3, 2, 4]) list([3, 3, 2]) list([4, 5, 2]) list([4, 2, 8, 5])]

np.arange()는 특정 값을 갖는 배열을 만들며, array에서 range()함수와 같은 역할을 한다.


In [54]:
np.arange(5)


Out[54]:
array([0, 1, 2, 3, 4])

In [55]:
np.arange(3,7,2)


Out[55]:
array([3, 5])

In [56]:
np.arange(3,4,2)


Out[56]:
array([3])

In [57]:
help(np.arange)


Help on built-in function arange in module numpy.core.multiarray:

arange(...)
    arange([start,] stop[, step,], dtype=None)
    
    Return evenly spaced values within a given interval.
    
    Values are generated within the half-open interval ``[start, stop)``
    (in other words, the interval including `start` but excluding `stop`).
    For integer arguments the function is equivalent to the Python built-in
    `range <http://docs.python.org/lib/built-in-funcs.html>`_ function,
    but returns an ndarray rather than a list.
    
    When using a non-integer step, such as 0.1, the results will often not
    be consistent.  It is better to use ``linspace`` for these cases.
    
    Parameters
    ----------
    start : number, optional
        Start of interval.  The interval includes this value.  The default
        start value is 0.
    stop : number
        End of interval.  The interval does not include this value, except
        in some cases where `step` is not an integer and floating point
        round-off affects the length of `out`.
    step : number, optional
        Spacing between values.  For any output `out`, this is the distance
        between two adjacent values, ``out[i+1] - out[i]``.  The default
        step size is 1.  If `step` is specified, `start` must also be given.
    dtype : dtype
        The type of the output array.  If `dtype` is not given, infer the data
        type from the other input arguments.
    
    Returns
    -------
    arange : ndarray
        Array of evenly spaced values.
    
        For floating point arguments, the length of the result is
        ``ceil((stop - start)/step)``.  Because of floating point overflow,
        this rule may result in the last element of `out` being greater
        than `stop`.
    
    See Also
    --------
    linspace : Evenly spaced numbers with careful handling of endpoints.
    ogrid: Arrays of evenly spaced numbers in N-dimensions.
    mgrid: Grid-shaped arrays of evenly spaced numbers in N-dimensions.
    
    Examples
    --------
    >>> np.arange(3)
    array([0, 1, 2])
    >>> np.arange(3.0)
    array([ 0.,  1.,  2.])
    >>> np.arange(3,7)
    array([3, 4, 5, 6])
    >>> np.arange(3,7,2)
    array([3, 5])

np.ones() 모든 값이 0으로 초기화해서 배열을 만든다. 
np.ones()와 np.zeros()에서 배열을 1차원 이상으로 만들 때 두 쌍의 괄호 세트가 필요하다.

In [58]:
np.ones(3)


Out[58]:
array([ 1.,  1.,  1.])

In [59]:
np.ones((3,4)) #1차원 이상으로 만들때 괄호를 감싸야 한다.


Out[59]:
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])

In [61]:
np.zeros(3)


Out[61]:
array([ 0.,  0.,  0.])

In [62]:
np.zeros(3, dtype=np.float32)


Out[62]:
array([ 0.,  0.,  0.], dtype=float32)

In [63]:
np.zeros(3, dtype=np.int)


Out[63]:
array([0, 0, 0])

In [65]:
np.eye(1)


Out[65]:
array([[ 1.]])

In [66]:
e = np.eye(1)

In [67]:
print(e.shape)


(1, 1)

In [68]:
e = np.eye(2)

In [69]:
print(e)


[[ 1.  0.]
 [ 0.  1.]]

In [70]:
print(e.shape)


(2, 2)

In [77]:
np.array([1,2]).shape


Out[77]:
(2,)

In [78]:
np.linspace(3,7,3)


Out[78]:
array([ 3.,  5.,  7.])

In [79]:
np.linspace(3,7,4)


Out[79]:
array([ 3.        ,  4.33333333,  5.66666667,  7.        ])

In [ ]: